<template>
  <el-card class="page-container">
    <template #header>
      <div class="card-header">
        <el-icon><Document /></el-icon>
        <span>{{ $route.meta.title }}</span>
      </div>
    </template>

    <!-- Simplified Query Form -->
    <div class="query-form">
      <el-input
          v-model="queryParams.name"
          placeholder="Name"
          clearable
          style="width: 200px; margin-right: 12px"
      />
      <el-select
          v-model="queryParams.status"
          placeholder="Status"
          clearable
          style="width: 120px; margin-right: 12px"
      >
        <el-option label="Active" value="1" />
        <el-option label="Inactive" value="0" />
      </el-select>
      <el-button type="primary" @click="handleQuery">Search</el-button>
      <el-button @click="resetQuery">Reset</el-button>
    </div>

    <!-- Clean Table -->
    <div class="table-wrapper">
      <el-table
          :data="tableData"
          style="width: 100%"
          stripe
          v-loading="loading"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="48" align="center" />
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="name" label="Name" min-width="160" />
        <el-table-column prop="status" label="Status" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === '1' ? 'success' : 'danger'" size="small">
              {{ row.status === '1' ? 'Active' : 'Inactive' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="Date" width="160" />
        <el-table-column label="Actions" width="120" align="center">
          <template #default="{ row }">
            <el-button size="small" plain @click="handleEdit(row)">Edit</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- Minimal Pagination -->
    <div class="pagination-wrapper">
      <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          background
      />
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Document } from '@element-plus/icons-vue'

interface TableItem {
  id: number
  name: string
  status: string
  createTime: string
}

const queryParams = ref({
  name: '',
  status: '',
  pageNum: 1,
  pageSize: 10
})

const tableData = ref<TableItem[]>([])
const total = ref(0)
const loading = ref(false)
const multipleSelection = ref<TableItem[]>([])

const fetchData = async () => {
  loading.value = true
  try {
    // Mock API call
    await new Promise(resolve => setTimeout(resolve, 300))

    // Generate mock data
    const mockData: TableItem[] = Array.from({ length: 35 }, (_, i) => ({
      id: i + 1,
      name: `Item ${i + 1}`,
      status: Math.random() > 0.5 ? '1' : '0',
      createTime: new Date().toLocaleDateString()
    }))

    // Filter data
    let filteredData = mockData.filter(item =>
        (!queryParams.value.name || item.name.includes(queryParams.value.name)) &&
        (!queryParams.value.status || item.status === queryParams.value.status)
    )

    total.value = filteredData.length
    const start = (queryParams.value.pageNum - 1) * queryParams.value.pageSize
    tableData.value = filteredData.slice(start, start + queryParams.value.pageSize)
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  fetchData()
}

const resetQuery = () => {
  queryParams.value = { name: '', status: '', pageNum: 1, pageSize: 10 }
  fetchData()
}

const handleSelectionChange = (val: TableItem[]) => {
  multipleSelection.value = val
}

const handleEdit = (row: TableItem) => {
  console.log('Edit:', row)
}

onMounted(fetchData)
</script>

<style scoped>
.page-container {
  border-radius: 8px;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
}

.query-form {
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.table-wrapper {
  margin: 12px 0;
  border-radius: 8px;
  overflow: hidden;
}

.pagination-wrapper {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-table__header) th {
  background-color: #f8f8f9;
}

:deep(.el-table) .el-button {
  padding: 5px 8px;
}
</style>